﻿#pragma once

#include "windows.h"

#include <string>
#include <iostream>

#include "Tasklet.h"

class GotoTasklet;

class Monster
{
public:
	Monster(void);
	Monster(const std::string& name, int pos);
	virtual ~Monster(void);

	void gotoAndEat(int tgt, int speed);
public:
	std::string name_;
	int pos_;

	GotoTasklet* gotoandeat_tasklet_;
};

class GotoTasklet : virtual public Tasklet<GotoTasklet>
{
public:
	GotoTasklet(Monster* self, int tgt, int speed)
		:Tasklet<GotoTasklet>(this)
		,self_(self)
		,tgt_(tgt)
		,speed_(speed)
	{
	}

	virtual ~GotoTasklet(){};
	
	virtual void run()
	{
		while(true)
		{
			self_->pos_ += speed_;
			// 到达目的地
			if(abs(tgt_ - self_->pos_) <= abs(speed_))
			{
				break;
			}
			std::cout << self_->name_ << " curr_pos = " << self_->pos_ << std::endl;
			// 切换回主 fiber
			yield();
		}
		std::cout << self_->name_ << " arrived." << std::endl;
		
		std::cout << self_->name_ << " eat." << std::endl;
	}
private:
	Monster* self_;
	int tgt_;
	int speed_;
};